local debug = require "debug"
local auth = require'auth'
require 'lfs'
require 'unity'
local mpi = require "luaMPI"
local nl = require"luaNumeric"

----------------------------------------------------------------------------------------------------------------------------------------
--初始化参数
local luaAuth={

};
----------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------
--创建表和创建元表
function luaAuth:new (o)
	o = o or {};
	self.__index = self;
	setmetatable(o,self)
	return o;
end
----------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------
--update  position 
function luaAuth:generate_code()	
	print( mpi.world.rank(),mpi.world.size() )
	mpi.world.barrier();
	local reqs = mpi.request_list:new();
	--
	vals = nl.new_svector(mpi.world.size());

	if(  mpi.world.rank()==0 ) then
		for jj = 1, mpi.world.size()-1 do
			val = nl.new_svector();
			req= mpi.world.irecv( jj,  jj , val);
			vals[jj+1] = val[1];
			reqs:add(req);
		end
	else
		local req_code = auth.get_auth_request_code();
		val = nl.new_svector();
		val:add( '  ' )
		req = mpi.world.isend(0,mpi.world.rank(),val);	
		reqs:add(req);	
	end
	mpi.wait_all(reqs);	
	mpi.world.barrier();	
	if(  mpi.world.rank()==0 ) then
		for jj = 1, mpi.world.size() do
			print( vals[jj] )
		end
	end
	print( 'rank:', mpi.world.rank() );
end
----------------------------------------------------------------------------------------------------------------------------------------


return luaAuth;